package top.infopub.io;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import top.infopub.io.impl.SimpleContentHandler;


/**
 * FileReader
 * @author Awoke
 * @version 2018年9月13日
 * @see FileReader
 * @since
 */
public abstract class FileReader {

    final Logger log = LoggerFactory.getLogger(this.getClass());

    ContentHandler<?> handler;

    protected FileLoader fileLoader;

    public FileReader(FileLoader fileLoader) {
        this.fileLoader = fileLoader;
    }

    public List<String> parse() {
        if (!can()) {
            throw new RuntimeException("cannot handler this,info:" + fileLoader.getInfo());
        }
        ContentHandler<List<String>> handler = new SimpleContentHandler();
        parse(handler);
        return handler.result();
    }

    public <T> T parse(ContentHandler<T> handler) {
        this.handler = handler;
        if (!can()) {
            throw new RuntimeException("cannot handler this,info:" + fileLoader.getInfo());
        }
        try {
            parse0(handler);
        }
        catch (Exception e) {
            log.error("", e);
        }
        return handler.result();
    }

    public abstract boolean can();

    protected abstract void parse0(ContentHandler<?> handler);

}
